package lena;

import java.awt.image.BufferedImage;
import lib.Actor;
import lib.ActorAddress;
import lib.Message;


public class MirrorActor extends Actor{

	public MirrorActor(ActorAddress superActor,ActorAddress myAddress) throws Exception {
		super(superActor, myAddress, "MirrorActor");
		startActor();
	}

	protected boolean standardRoutine() throws Exception {
		this.standardRoutineIsUsed=false;
		return true;
	}

	protected boolean handleMessage(Message msg) throws Exception {
		if (msg.getOperation().equals("Terminate")) {
			System.out.println("MirrorActor will halt");
			return false;
		}

		System.out.println("MirrorActor : Img received.");
		ImgChunk chunk = (ImgChunk) msg.getContent();
		BufferedImage temp = chunk.getImage();	
		BufferedImage newimg = new BufferedImage(temp.getWidth(),temp.getHeight(),temp.getType());
		int rgb;
		for(int i=0;i<temp.getWidth();i++){
			for(int j=0;j<temp.getHeight();j++){
				rgb=temp.getRGB(i,j);
				newimg.setRGB(newimg.getWidth()-i-1,j,rgb);
			}
		}
		ImgChunk ToBeSent = new ImgChunk(chunk.getPart(),chunk.getNbPartTotal(),newimg,chunk.getiDImage());
		sendMsg(new Message(this.myAddress, "MirrorActor","RecomposerActor", "", ToBeSent));
		return true;
	}

}
